package com.videoaggre.zcl.common

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import android.view.View
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.videoaggre.zcl.App
import com.videoaggre.zcl.utils.Utils
import java.io.Serializable

val Int.dp get() = (this * App.app.resources.displayMetrics.density + 0.5f).toInt()
val Int.drawable get() = ContextCompat.getDrawable(App.app, this)
fun dp2px(dpValue: Int) = (dpValue * App.app.resources.displayMetrics.density + 0.5f).toInt()
fun toastMessageLong(strIdes: Int) = Utils.toastMessageShort(App.app.getString(strIdes))
fun toastMessageLong(str: String?) = Utils.toastMessageShort(str)
fun getString(strIdes: Int) = App.app.getString(strIdes)
fun getString(strIdes: Int, vararg formatArgs: Any) = App.app.getString(strIdes, *formatArgs)
fun bindViewClick(listener: View.OnClickListener, vararg views: View) {
    for (it in views) {
        it.setOnClickListener(listener)
    }
}

fun java.io.Closeable.tryClose() {
    try {
        close()
    } catch (e: Exception) {

    }
}

var View.changeVisibility: Boolean
    get() = true
    set(value) {
        visibility = if (value) View.VISIBLE else View.GONE
    }

inline fun <reified T : Activity> Activity.startKtxActivity(flags: Int? = null, extra: Bundle? = null) = startActivity(getIntent<T>(flags, extra))
inline fun <reified T : Activity> Fragment.startKtxActivity(flags: Int? = null, extra: Bundle? = null) = startActivity(activity?.getIntent<T>(flags, extra))
inline fun <reified T : Activity> Context.startKtxActivity(flags: Int? = null, extra: Bundle? = null) = startActivity(getIntent<T>(flags, extra))
inline fun <reified T : Activity> Activity.startKtxActivity(flags: Int? = null, extra: Bundle? = null, value: Pair<String, Any>?) = startActivity(getIntent<T>(flags, extra, arrayListOf(value)))
inline fun <reified T : Activity> Fragment.startKtxActivity(flags: Int? = null, extra: Bundle? = null, value: Pair<String, Any>?) = startActivity(activity?.getIntent<T>(flags, extra, arrayListOf(value)))
inline fun <reified T : Activity> Context.startKtxActivity(flags: Int? = null, extra: Bundle? = null, value: Pair<String, Any>?) = startActivity(getIntent<T>(flags, extra, arrayListOf(value)))
inline fun <reified T : Activity> Activity.startKtxActivity(flags: Int? = null, extra: Bundle? = null, values: List<Pair<String, Any>?>?) = startActivity(getIntent<T>(flags, extra, values))
inline fun <reified T : Activity> Fragment.startKtxActivity(flags: Int? = null, extra: Bundle? = null, values: List<Pair<String, Any>?>?) = startActivity(activity?.getIntent<T>(flags, extra, values))
inline fun <reified T : Activity> Context.startKtxActivity(flags: Int? = null, extra: Bundle? = null, values: List<Pair<String, Any>?>?) = startActivity(getIntent<T>(flags, extra, values))
inline fun <reified T : Activity> Activity.startKtxActivityForResult(requestCode: Int, flags: Int? = null, extra: Bundle? = null) = startActivityForResult(getIntent<T>(flags, extra), requestCode)
inline fun <reified T : Activity> Fragment.startKtxActivityForResult(requestCode: Int, flags: Int? = null, extra: Bundle? = null) = startActivityForResult(activity?.getIntent<T>(flags, extra), requestCode)
inline fun <reified T : Context> Context.getIntent(flags: Int?, extra: Bundle?): Intent = Intent(this, T::class.java).apply {
    flags?.let { setFlags(flags) }
    extra?.let { putExtras(extra) }
}

inline fun <reified T : Context> Context.getIntent(flags: Int?, extra: Bundle?, pairs: List<Pair<String, Any>?>?): Intent = Intent(this, T::class.java).apply {
    flags?.let { setFlags(flags) }
    extra?.let { putExtras(extra) }
    pairs?.let {
        for (pair in pairs)
            pair?.let {
                val name = pair.first
                when (val value = pair.second) {
                    is Int -> putExtra(name, value)
                    is Byte -> putExtra(name, value)
                    is Char -> putExtra(name, value)
                    is Short -> putExtra(name, value)
                    is Boolean -> putExtra(name, value)
                    is Long -> putExtra(name, value)
                    is Float -> putExtra(name, value)
                    is Double -> putExtra(name, value)
                    is String -> putExtra(name, value)
                    is CharSequence -> putExtra(name, value)
                    is Parcelable -> putExtra(name, value)
                    is Array<*> -> putExtra(name, value)
                    is ArrayList<*> -> putExtra(name, value)
                    is Serializable -> putExtra(name, value)
                    is BooleanArray -> putExtra(name, value)
                    is ByteArray -> putExtra(name, value)
                    is ShortArray -> putExtra(name, value)
                    is CharArray -> putExtra(name, value)
                    is IntArray -> putExtra(name, value)
                    is LongArray -> putExtra(name, value)
                    is FloatArray -> putExtra(name, value)
                    is DoubleArray -> putExtra(name, value)
                    is Bundle -> putExtra(name, value)
                    is Intent -> putExtra(name, value)
                    else -> {
                    }
                }
            }
    }
}
